package com.ssm.subject.domain.handler.subejct;

import com.ssm.subject.common.enums.IsDeletedFlagEnum;
import com.ssm.subject.common.enums.SubjectInfoTypeEnum;
import com.ssm.subject.domain.bo.SubjectAnswerBO;
import com.ssm.subject.domain.bo.SubjectInfoBO;
import com.ssm.subject.domain.bo.SubjectOptionBO;
import com.ssm.subject.domain.convert.SubjectMultipleConvert;
import com.ssm.subject.infra.basic.entity.SubjectMultiple;
import com.ssm.subject.infra.basic.service.SubjectMappingService;
import com.ssm.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 多选策略类
 */
@Component
public class MultipleTypeHandle implements SubejctTypeHandler{
    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //多选题目的插入
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        List<SubjectMultiple> subjectMultipleList = new ArrayList<>();
        if(CollectionUtils.isEmpty(optionList)) {
            return;
        }
        optionList.forEach(subjectAnswerBO -> {
            SubjectMultiple subjectMultiple = SubjectMultipleConvert.INSTANCE.subjectAnswerBoToMultiple(subjectAnswerBO);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultipleList.add(subjectMultiple);
        });
        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjetcId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjetcId);
        subjectMultiple.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.query(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOS = SubjectMultipleConvert.INSTANCE.subjectMultipleListToAnswerBoList(subjectMultipleList);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
